//
//  HMNewFeatureController.swift
//  weibo10
//
//  Created by mac on 15/10/20.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SnapKit

private let newFeatureReuseIdentifier = "Cell"
private let NewFeatureCount = 4
class HMNewFeatureController: UICollectionViewController {

    init(){
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView?.backgroundColor = UIColor.yellowColor()
        // 注册可重用cell
        self.collectionView!.registerClass(HMNewFeatureCell.self, forCellWithReuseIdentifier: newFeatureReuseIdentifier)

    }
}
// MARK: UICollectionViewDataSource
extension HMNewFeatureController{
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return NewFeatureCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(newFeatureReuseIdentifier, forIndexPath: indexPath) as! HMNewFeatureCell
        
        cell.imageIndex = indexPath.row
        return cell
    }
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        // 计算页数
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        // 判断是否为最后一页
        if page != NewFeatureCount - 1 {
            return
        }
        // 取得当前cell
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! HMNewFeatureCell
        cell.showAnimate()
        
    }
}
/// 自定义cell
class HMNewFeatureCell: UICollectionViewCell {
    /// 图像属性
    private var imageIndex: Int = 0{
        didSet{
            backImageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startBtn.hidden = true
        }
        
    }
    @objc private func startBtnClick(){
        /// 通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func showAnimate(){
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.userInteractionEnabled = false
        UIView.animateWithDuration(1.0,
            delay: 0,
            usingSpringWithDamping: 0.5,
            initialSpringVelocity: 10,
            options: [], animations: { () -> Void in
                self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startBtn.userInteractionEnabled = true
        }
    }
    private func setupUI(){
        // 1. 添加控件
        addSubview(backImageView)
        addSubview(startBtn)
        // 设置圆角
        startBtn.layer.cornerRadius = 5
        startBtn.layer.masksToBounds = true
        // 2. 自动布局
        backImageView.frame = bounds
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(backImageView.snp_centerX)
            make.bottom.equalTo(backImageView.snp_bottom).multipliedBy(0.7)
        }
        // 不能单纯的在这设置隐藏，会出现cell重用的现象
        startBtn.hidden = true
        // 给按钮注册点击事件
        startBtn.addTarget(self, action: Selector("startBtnClick"), forControlEvents: .TouchUpInside)
        
    }
    /// 懒加载控件
    // 1.背景图
    private lazy var backImageView: UIImageView = UIImageView()
    // 2.开始体验
    private lazy var startBtn:UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), imageName: "new_feature_finish_button")
}